﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DAL;
using DTO;

namespace BLL
{
    public class AppointmentBLL
    {
        private static int appointmentid = 0;
        HospitalDataContext DB = new HospitalDataContext();
        public List<Appointment> SelectAll()
        {
            return DB.Appointments.ToList();
        }
        public List<Appointment> SelectDesc()
        {
            return DB.Appointments.OrderByDescending(a => a.AppointmentID).ToList();
        }
        public List<Appointment> Search(string key)
        {
            return DB.Appointments.Where(app => app.AppointmentID.Contains(key)).ToList();
        }
        public string GetPatientID(string appointID)
        {
            return DB.Appointments.Where(app => app.AppointmentID == appointID).FirstOrDefault().PatientID;
        }
        public IQueryable SearchwithStafforPatient(string key)
        {
            return DB.Appointments.Join(
                    DB.Staffs,
                    a => a.StaffID,
                    b => b.StaffID,
                    (a, b) => new
                    {
                        AppointmentID = a.AppointmentID,
                        StaffID = a.StaffID,
                        PatientID = a.PatientID,
                        Date = a.Date,
                        Time = a.Time,
                        Room = a.Room,
                        Description = a.Description,
                        Status = a.Status,
                        sFirstName = b.FirstName,
                        sLastName = b.LastName
                    }
                ).Join(
                    DB.Patients,
                    a => a.PatientID,
                    b => b.PatientID,
                    (a, b) => new
                    {
                        AppointmentID = a.AppointmentID,
                        StaffID = a.StaffID,
                        PatientID = a.PatientID,
                        Date = a.Date,
                        Time = a.Time,
                        Room = a.Room,
                        Description = a.Description,
                        Status = a.Status,
                        sFirstName = a.sFirstName,
                        sLastName = a.sLastName,
                        pFirstName = b.FirstName,
                        pLastName = b.LastName

                    }
                ).Where(bang => bang.sLastName.Contains(key) || bang.sFirstName.Contains(key) || bang.pFirstName.Contains(key) || bang.pLastName.Contains(key)).AsQueryable();
        }
        public String Insert(AppointmentDTO nhap)
        {
            try
            {
                Appointment app = new Appointment();
                app.AppointmentID = (appointmentid++).ToString();
                app.StaffID = nhap.Staffid;
                app.PatientID = nhap.Patientid;
                app.Date = nhap.Date;
                app.Time = nhap.Time;
                app.Room = nhap.Room;               
                app.Description = nhap.Description;
                app.Status = nhap.Status;

                DB.Appointments.InsertOnSubmit(app);
                DB.SubmitChanges();
                return "success";
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }
        public string Update(AppointmentDTO nhap)
        {
            try
            {
                Appointment app = DB.Appointments.Where(a => a.AppointmentID == nhap.Appointmentid).FirstOrDefault();
                app.StaffID = nhap.Staffid;
                app.PatientID = nhap.Patientid;
                app.Date = nhap.Date;
                app.Time = nhap.Time;
                app.Room = nhap.Room;
                app.Description = nhap.Description;
                app.Status = nhap.Status;

                DB.SubmitChanges();
                return "success";
            }
            catch (Exception e)
            {
                return e.Message;
            }
        }
        public string UpdateOnListWait(string appointmentid)
        {
            try
            {
                Appointment app = DB.Appointments.Where(a => a.AppointmentID == appointmentid).FirstOrDefault();
                app.Status = "listwait";

                DB.SubmitChanges();
                return "success";
            }
            catch (Exception e)
            {
                return e.Message;
            }

        }
        public string UpdateTo_OutPatient(string appointmentid)
        {
            try
            {
                Appointment app = DB.Appointments.Where(a => a.AppointmentID == appointmentid).FirstOrDefault();
                app.Status = "outpatient";

                DB.SubmitChanges();
                return "success";
            }
            catch (Exception e)
            {
                return e.Message;
            }

        }
    }
}
